package com.cc.sanjieduan.service.impl;

import com.cc.sanjieduan.events.MenuChangeEvent;
import com.cc.sanjieduan.mapper.MenuMapper;
import com.cc.sanjieduan.pojo.BaseUser;
import com.cc.sanjieduan.pojo.Menu;
import com.cc.sanjieduan.service.MenuService;
import com.cc.sanjieduan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private  MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public void updateMenu(Menu menu) {
        BaseUser user = SecurityUtils.getUser();
        menu.setUpdateBy(user.getUsername());
        menu.setUpdateTime(new Date());
        menuMapper.updateMenu(menu);
        context.publishEvent(new MenuChangeEvent());
    }

    @Override
    public void updateMenuEnable(String enable, String menuId) {
        menuMapper.updateMenuEnable(enable, menuId);
        context.publishEvent(new MenuChangeEvent());

    }
}
